package com.jingze.plugins.monitor.customize;


import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class doRun {

    //togetherNumLights:同时亮灯数量
    //repeatNum:循环次数
    //runningSpeed:跑灯速度(单位：秒)
    //stringJsonLights:执行队列("{\"id\":4,\"lightName\":4,\"lightColour0\":5,\"lightColour1\":0,\"lightColour2\":0,\"lightNow\":0},")
    public int runLights(String artNetAdress,int togetherNumLights,int repeatNum,double runningSpeed,String stringJsonLights) throws InterruptedException {

        for (int irepeat = 0 ; irepeat < repeatNum ; irepeat++){
            Thread.sleep((long) (runningSpeed*1000));
            List<Light> Lights = JSONObject.parseArray(stringJsonLights,Light.class);

            ArrayList lightWaitListID = new ArrayList();
            ArrayList<List<Light>> lightComplete = new ArrayList();
            ArrayList lightWaitList = new ArrayList();
            JArtNet artNet1 = new JArtNet(artNetAdress);
            JArtNet artNet2 = new JArtNet(artNetAdress);
            JArtNet artNet3 = new JArtNet(artNetAdress);
            JArtNet artNet4 = new JArtNet(artNetAdress);
            JArtNet artNet5 = new JArtNet(artNetAdress);
            JArtNet artNet6 = new JArtNet(artNetAdress);
            JArtNet artNet7 = new JArtNet(artNetAdress);
            JArtNet artNet8 = new JArtNet(artNetAdress);
            for (int i = 0 ; i < Lights.size() ; i++){

                //执行走灯
                Thread.sleep((long) (runningSpeed*1000));
                Light light=Lights.get(i);
                light.setLightName0(light.getLightName());
                light.setLightName1(light.getLightName());
                light.setLightName2(light.getLightName());

                lightWaitList.add(light);
                //System.out.println("lightWaitList:"+lightWaitList.size());
                if (light.getLightNow()==1)
                {

                    List<Light> listTemp=new ArrayList();
                    listTemp.addAll(lightWaitList);
                    //System.out.println("listTemp:"+JSONObject.toJSON(listTemp));
                    lightComplete.add(listTemp);

                    //System.out.println("----------"+lightComplete.size()+"----------------");

                    //执行关灯脚本
                    for	(int i1=0;i1<lightComplete.size();i1++)
                    {
                        if ((lightComplete.size() > togetherNumLights ) && (i1 < (lightComplete.size() - togetherNumLights)))
                        {
                            for (int i2=0;i2<lightComplete.get(i1).size();i2++)
                            {
                                //System.out.println("执行关灯脚本i1:"+i1+":"+JSONObject.toJSONString(lightComplete.get(i1).get(i2)));
                                int channgel=lightComplete.get(i1).get(i2).getChannel();
                                switch (channgel)
                                {

                                    case 8:
                                        artNet8.set(lightComplete.get(i1).get(i2).getLightName0(),0);
                                        artNet8.set(lightComplete.get(i1).get(i2).getLightName1(),0);
                                        artNet8.set(lightComplete.get(i1).get(i2).getLightName2(),0);
                                        artNet8.send(lightComplete.get(i1).get(i2).getChannel());
                                        break;
                                    case 7:
                                        artNet7.set(lightComplete.get(i1).get(i2).getLightName0(),0);
                                        artNet7.set(lightComplete.get(i1).get(i2).getLightName1(),0);
                                        artNet7.set(lightComplete.get(i1).get(i2).getLightName2(),0);
                                        artNet7.send(lightComplete.get(i1).get(i2).getChannel());
                                        break;
                                    case 6:
                                        artNet6.set(lightComplete.get(i1).get(i2).getLightName0(),0);
                                        artNet6.set(lightComplete.get(i1).get(i2).getLightName1(),0);
                                        artNet6.set(lightComplete.get(i1).get(i2).getLightName2(),0);
                                        artNet6.send(lightComplete.get(i1).get(i2).getChannel());
                                        break;
                                    case 5:
                                        artNet5.set(lightComplete.get(i1).get(i2).getLightName0(),0);
                                        artNet5.set(lightComplete.get(i1).get(i2).getLightName1(),0);
                                        artNet5.set(lightComplete.get(i1).get(i2).getLightName2(),0);
                                        artNet5.send(lightComplete.get(i1).get(i2).getChannel());
                                        break;
                                    case 4:
                                        artNet4.set(lightComplete.get(i1).get(i2).getLightName0(),0);
                                        artNet4.set(lightComplete.get(i1).get(i2).getLightName1(),0);
                                        artNet4.set(lightComplete.get(i1).get(i2).getLightName2(),0);
                                        artNet4.send(lightComplete.get(i1).get(i2).getChannel());
                                        break;
                                    case 3:
                                        artNet3.set(lightComplete.get(i1).get(i2).getLightName0(),0);
                                        artNet3.set(lightComplete.get(i1).get(i2).getLightName1(),0);
                                        artNet3.set(lightComplete.get(i1).get(i2).getLightName2(),0);
                                        artNet3.send(lightComplete.get(i1).get(i2).getChannel());
                                        break;
                                    case 2:
                                        artNet2.set(lightComplete.get(i1).get(i2).getLightName0(),0);
                                        artNet2.set(lightComplete.get(i1).get(i2).getLightName1(),0);
                                        artNet2.set(lightComplete.get(i1).get(i2).getLightName2(),0);
                                        artNet2.send(lightComplete.get(i1).get(i2).getChannel());
                                        break;
                                    case 1:
                                        artNet1.set(lightComplete.get(i1).get(i2).getLightName0(),0);
                                        artNet1.set(lightComplete.get(i1).get(i2).getLightName1(),0);
                                        artNet1.set(lightComplete.get(i1).get(i2).getLightName2(),0);
                                        artNet1.send(lightComplete.get(i1).get(i2).getChannel());
                                        break;
                                    default :
                                        artNet1.set(lightComplete.get(i1).get(i2).getLightName0(),0);
                                        artNet1.set(lightComplete.get(i1).get(i2).getLightName1(),0);
                                        artNet1.set(lightComplete.get(i1).get(i2).getLightName2(),0);
                                        artNet1.send(lightComplete.get(i1).get(i2).getChannel());
                                        break;
                                }
                            }

                            lightComplete.remove(i1);
                            //System.out.println("执行关灯脚本i1:"+i1);
                        }

                    }

                    for	(int i1=0;i1<lightWaitList.size(); i1++)
                    {
                        //执行亮灯脚本
                        Light light1temp = (Light)lightWaitList.get(i1);
                        switch (light1temp.getChannel())
                        {
                            case 8:
                                artNet8.set(light1temp.getLightName0(),light1temp.getLightColour0());
                                artNet8.set(light1temp.getLightName1(),light1temp.getLightColour1());
                                artNet8.set(light1temp.getLightName2(),light1temp.getLightColour2());
                                artNet8.send(light1temp.getChannel());
                                break;
                            case 7:
                                artNet7.set(light1temp.getLightName0(),light1temp.getLightColour0());
                                artNet7.set(light1temp.getLightName1(),light1temp.getLightColour1());
                                artNet7.set(light1temp.getLightName2(),light1temp.getLightColour2());
                                artNet7.send(light1temp.getChannel());
                                break;
                            case 6:
                                artNet6.set(light1temp.getLightName0(),light1temp.getLightColour0());
                                artNet6.set(light1temp.getLightName1(),light1temp.getLightColour1());
                                artNet6.set(light1temp.getLightName2(),light1temp.getLightColour2());
                                artNet6.send(light1temp.getChannel());
                                break;
                            case 5:
                                artNet5.set(light1temp.getLightName0(),light1temp.getLightColour0());
                                artNet5.set(light1temp.getLightName1(),light1temp.getLightColour1());
                                artNet5.set(light1temp.getLightName2(),light1temp.getLightColour2());
                                artNet5.send(light1temp.getChannel());
                                break;
                            case 4:
                                artNet4.set(light1temp.getLightName0(),light1temp.getLightColour0());
                                artNet4.set(light1temp.getLightName1(),light1temp.getLightColour1());
                                artNet4.set(light1temp.getLightName2(),light1temp.getLightColour2());
                                artNet4.send(light1temp.getChannel());
                                break;
                            case 3:
                                artNet3.set(light1temp.getLightName0(),light1temp.getLightColour0());
                                artNet3.set(light1temp.getLightName1(),light1temp.getLightColour1());
                                artNet3.set(light1temp.getLightName2(),light1temp.getLightColour2());
                                artNet3.send(light1temp.getChannel());
                                break;
                            case 2:
                                artNet2.set(light1temp.getLightName0(),light1temp.getLightColour0());
                                artNet2.set(light1temp.getLightName1(),light1temp.getLightColour1());
                                artNet2.set(light1temp.getLightName2(),light1temp.getLightColour2());
                                artNet2.send(light1temp.getChannel());
                                break;
                            case 1:
                                artNet1.set(light1temp.getLightName0(),light1temp.getLightColour0());
                                artNet1.set(light1temp.getLightName1(),light1temp.getLightColour1());
                                artNet1.set(light1temp.getLightName2(),light1temp.getLightColour2());
                                artNet1.send(light1temp.getChannel());
                                break;
                            default:
                                artNet1.set(light1temp.getLightName0(),light1temp.getLightColour0());
                                artNet1.set(light1temp.getLightName1(),light1temp.getLightColour1());
                                artNet1.set(light1temp.getLightName2(),light1temp.getLightColour2());
                                artNet1.send(light1temp.getChannel());
                                break;
                        }
                        //System.out.println("执行亮灯脚本:i1:"+i1+JSONObject.toJSONString(light1temp));

                    }
                    //System.out.println("lightWaitList-info:"+JSONObject.toJSON(lightWaitList));

                    lightWaitList.clear();
                }
            }

        }

        return 1;
    }
}
